<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .nav {
        position: relative;
        width: 500px;
        height: 400px;
        margin: 0 auto;
        border: 1px solid blue;
      }
      .chatting {
        width: 450px;
        height: 250px;
        overflow-y: auto;
        margin: 0 auto;
        margin-top: 10px;
        border: 1px solid #cecece;
      }
      textarea {
        position: absolute;
        right: 25px;
        bottom: 30px;
        width: 446px;
        height: 80px;
        /* padding: 0; */
      }
    </style>
  </head>
  <body>
    <div class="nav">
      <div class="chatting"></div>
      <textarea placeholder="你好，按下Ctrl+回车发送！"></textarea>
    </div>
  </body>
  <script>
    window.onload = function () {
      var oDiv = document.querySelector(".chatting");
      var oTextarea = document.querySelector("textarea");
      var aP = document.getElementsByTagName("p");
      oTextarea.onkeydown = function (ev) {
        // console.log("输入成功");
        if (ev.ctrlKey && ev.keyCode == 13) {
          console.log("输入成功");
          oDiv.innerHTML += "<p>" + oTextarea.value + "</p>";
          oTextarea.value = "";
        }
        oDiv.scrollTop = oDiv.scrollHeight;
      };
    };
    //自动聚焦最后一个p标签
  </script>
</html>
